package com.cdu.ai_intelligent_teaching_and_training_system.controller;

import com.cdu.ai_intelligent_teaching_and_training_system.dto.ApiResponse;
import com.cdu.ai_intelligent_teaching_and_training_system.entity.Course;
import com.cdu.ai_intelligent_teaching_and_training_system.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/courses")
@CrossOrigin(origins = "*")
public class CourseController {
    
    @Autowired
    private CourseService courseService;
    
    @GetMapping
    public ResponseEntity<ApiResponse<List<Course>>> getAllCourses() {
        try {
            List<Course> courses = courseService.findAll();
            return ResponseEntity.ok(ApiResponse.success(courses));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取课程列表失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Course>> getCourseById(@PathVariable Long id) {
        try {
            Course course = courseService.findById(id).orElse(null);
            if (course == null) {
                return ResponseEntity.ok(ApiResponse.error("课程不存在"));
            }
            return ResponseEntity.ok(ApiResponse.success(course));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取课程详情失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/teacher/{teacherId}")
    public ResponseEntity<ApiResponse<List<Course>>> getCoursesByTeacher(@PathVariable Long teacherId) {
        try {
            List<Course> courses = courseService.findByTeacherId(teacherId);
            return ResponseEntity.ok(ApiResponse.success(courses));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取教师课程失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/search")
    public ResponseEntity<ApiResponse<List<Course>>> searchCourses(@RequestParam String name) {
        try {
            List<Course> courses = courseService.findByNameContaining(name);
            return ResponseEntity.ok(ApiResponse.success(courses));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("搜索课程失败：" + e.getMessage()));
        }
    }
    
    @PostMapping
    public ResponseEntity<ApiResponse<Course>> createCourse(@Valid @RequestBody Course course) {
        try {
            Course savedCourse = courseService.save(course);
            return ResponseEntity.ok(ApiResponse.success("课程创建成功", savedCourse));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("创建课程失败：" + e.getMessage()));
        }
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Course>> updateCourse(@PathVariable Long id, @Valid @RequestBody Course course) {
        try {
            Course existingCourse = courseService.findById(id).orElse(null);
            if (existingCourse == null) {
                return ResponseEntity.ok(ApiResponse.error("课程不存在"));
            }
            
            course.setCourseId(id);
            Course updatedCourse = courseService.save(course);
            return ResponseEntity.ok(ApiResponse.success("课程更新成功", updatedCourse));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("更新课程失败：" + e.getMessage()));
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<String>> deleteCourse(@PathVariable Long id) {
        try {
            Course existingCourse = courseService.findById(id).orElse(null);
            if (existingCourse == null) {
                return ResponseEntity.ok(ApiResponse.error("课程不存在"));
            }
            
            courseService.deleteById(id);
            return ResponseEntity.ok(ApiResponse.success("课程删除成功", null));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("删除课程失败：" + e.getMessage()));
        }
    }
} 